package org.mspring.mlog.freemarker.directive;

import freemarker.core.Environment;
import freemarker.template.ObjectWrapper;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import org.mspring.mlog.entity.LinkEntity;
import org.mspring.mlog.service.LinkEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 友情链接列表
 *
 * @author Gao Youbo
 * @since 2015-01-24 13:32
 */
@Component
public class LinkListDirectiveModel implements TemplateDirectiveModel {
    private LinkEntityService linkEntityService;

    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        List<LinkEntity> linkList = linkEntityService.listAll();
        env.setVariable("linkList", ObjectWrapper.DEFAULT_WRAPPER.wrap(linkList));
        body.render(env.getOut());
    }

    @Autowired
    public void setLinkEntityService(LinkEntityService linkEntityService) {
        this.linkEntityService = linkEntityService;
    }
}
